using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace JHEMR.EMREdit
{
    public partial class frmEMRBasket : Form
    {
        public frmEMRBasket()
        {
            InitializeComponent();
        }

        private void frmEMRBasket_Load(object sender, EventArgs e)
        {
            // 初始化数据
            LoadVitalSigns();
            LoadMedicalRecords();
        }

        private void LoadVitalSigns()
        {
            DataSet objDataSetVitalSigns = new DataSet();
            objDataSetVitalSigns = getVitalSignsRec();
            if (objDataSetVitalSigns.Tables.Count > 0)
            {
                DataTable objTable = objDataSetVitalSigns.Tables[0];
                dgvVitalSigns.DataSource = objTable.DefaultView;
            }
        }

        private void LoadMedicalRecords()
        {
            emrpadMrBrief.PadShowToolbar(false);
            emrpadMrBrief.PadsetUsePaperEmptyMargin(true);
            emrpadMrBrief.PadsetRulerVisible(false);
            emrpadMrBrief.PadsetUsePadAccelerator(true);
            
            object[] strArgs = new object[3];
            strArgs[0] = 0;
            strArgs[1] = "blzy";
            strArgs[2] = "";
            
            if (EMRArchiveAdaperUse.retrieveEmrFile(strArgs))
            {
                if (emrpadMrBrief.PadOpenFile(EmrSysPubVar.getTempFileFullName()))
                {
                    // 成功打开文件
                }
            }
        }

        private void dgvLabTestMaster_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvLabTestMaster.SelectedRows.Count > 0)
            {
                try
                {
                    DataGridViewRow objCurRow = dgvLabTestMaster.SelectedRows[0];
                    DataSet objDataSet = EmrSysWebservicesUse.myEmrGetLabReportDataSet(objCurRow.Cells["ORDER_SN"].Value.ToString().Trim());
                    
                    if (objDataSet.Tables.Count > 0)
                    {
                        DataTable objTable = objDataSet.Tables[0];
                        foreach (DataRow drCurrent in objTable.Rows)
                        {
                            if (drCurrent["status"] != DBNull.Value)
                            {
                                if (drCurrent["status"].ToString() == "H")
                                    drCurrent["result_mark"] = "↑";
                                else if (drCurrent["status"].ToString() == "L")
                                    drCurrent["result_mark"] = "↓";
                            }
                        }
                        dgvLabTestItems.DataSource = objTable.DefaultView;
                    }
                }
                catch (System.Exception E)
                {
                    EmrSysPubFunction.DoWithSystemException(E);
                }
            }
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            int nTabPageIndex = tabBasket.SelectedIndex;
            switch (nTabPageIndex)
            {
                case 0: // 验证
                    WriteLabResults();
                    break;
                case 1: // 结构
                    WriteOrders();
                    break;
                case 2: // 考勤
                    WriteExamResults();
                    break;
                case 3: // 记录
                    WriteDiagnoses();
                    break;
                case 4: // 资源子网
                    WriteVitalSigns();
                    break;
            }
        }

        private void WriteLabResults()
        {
            txtLab.Text = "";
            if (dgvLabTestItems.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgvLabTestItems.Rows.Count; i++)
                {
                    DataGridViewRow objCurRow = dgvLabTestItems.Rows[i];
                    if (objCurRow.Selected)
                    {
                        if (txtLab.Text.Length > 0) txtLab.Text += "、";
                        txtLab.Text += objCurRow.Cells["REPORT_ITEM_NAME"].Value.ToString().Trim() + 
                                     objCurRow.Cells["RESULT"].Value.ToString().Trim() + 
                                     objCurRow.Cells["UNITS"].Value.ToString().Trim();
                    }
                }
            }
            
            if (txtLab.Text.Length > 0)
            {
                CopyToClipboard(txtLab.Text);
            }
        }

        private void WriteOrders()
        {
            string strContent = "";
            string ls_dosage, ls_dosage_units;
            
            if (dgvOrders.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgvOrders.Rows.Count; i++)
                {
                    DataGridViewRow objCurRow = dgvOrders.Rows[i];
                    if (objCurRow.Selected)
                    {
                        if (strContent.Length > 0) strContent += "，";
                        ls_dosage = objCurRow.Cells["DOSAGE"].Value.ToString();
                        ls_dosage_units = objCurRow.Cells["DOSAGE_UNITS"].Value.ToString();
                        ls_dosage = ls_dosage + ls_dosage_units;
                        strContent += objCurRow.Cells["ORDER_TEXT"].Value.ToString();
                        if (ls_dosage.Length > 0)
                            strContent += "(" + ls_dosage + ")";
                    }
                }
            }
            
            if (strContent.Length > 0)
            {
                CopyToClipboard(strContent);
            }
        }

        private void WriteExamResults()
        {
            if (txtExam.SelectedText.Length > 0 || txtExam.Text.Length > 0)
            {
                txtLab.Text = "";
                txtLab.Text = txtExam.SelectedText;
                if (txtLab.Text.Length == 0)
                    txtLab.Text = txtExam.Text;
                string strForCopy = txtLab.Text;
                strForCopy = strForCopy.Replace("\r", "");
                strForCopy = strForCopy.Replace("\n", "");
                CopyToClipboard(strForCopy);
            }
        }

        private void WriteDiagnoses()
        {
            string strContent = "";
            if (dgvPatDiagnose.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgvPatDiagnose.Rows.Count; i++)
                {
                    DataGridViewRow objCurRow = dgvPatDiagnose.Rows[i];
                    if (objCurRow.Selected)
                    {
                        if (strContent.Length > 0) strContent += "、";
                        strContent += objCurRow.Cells["记录名称"].Value.ToString();
                    }
                }
            }
            
            if (strContent.Length > 0)
            {
                CopyToClipboard(strContent);
            }
        }

        private void WriteVitalSigns()
        {
            string strContent = "";
            if (dgvVitalSigns.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgvVitalSigns.Rows.Count; i++)
                {
                    DataGridViewRow objCurRow = dgvVitalSigns.Rows[i];
                    if (objCurRow.Selected)
                    {
                        if (strContent.Length > 0) strContent += "、";
                        strContent += objCurRow.Cells["名称"].Value.ToString() + 
                                    objCurRow.Cells["值"].Value.ToString() + 
                                    objCurRow.Cells["单位"].Value.ToString();
                    }
                }
            }
            
            if (strContent.Length > 0)
            {
                CopyToClipboard(strContent);
            }
        }

        private void CopyToClipboard(string content)
        {
            Clipboard.SetDataObject(content, false);
            m_objPad.PadSetPasteMode(2); // 设置为粘贴文本格式
            m_objPad.PadEditPaste();
            m_objPad.PadSetPasteMode(1); // 设置为粘贴考勤格式
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvExam_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvExam.SelectedRows.Count > 0)
            {
                DataGridViewRow objCurRow = dgvExam.SelectedRows[0];
                if (objCurRow.Cells["RESULT_STATUS"].Value.ToString() != "采集")
                {
                    txtExam.Text = "";
                    return;
                }
                
                string strExamNo = objCurRow.Cells["EXAM_NO"].Value.ToString().Trim();
                DataSet dtExamReport = EmrSysWebservices.EmrSysWebservicesUse.myEmrGetExamReport(strExamNo);
                
                if (dtExamReport.Tables[0].Rows.Count == 1)
                {
                    string strExamReport = dtExamReport.Tables[0].Rows[0]["exam_report"].ToString();
                    txtExam.Text = strExamReport;
                }
            }
        }

        private void btnMrBrief_Click(object sender, EventArgs e)
        {
            // 保存医疗记录摘要
            if (!emrpadMrBrief.PadFileSaveAs(EmrSysPubVar.getTempFileFullName(), 0, 0))
            {
                MessageBox.Show("保存文件出错。");
                return;
            }
            
            // 传送文件到服务器
            object[] strArgs = new object[3];
            strArgs[0] = 0;
            strArgs[1] = "blzy";
            strArgs[2] = EmrSysPubVar.getCurPatientID();
            
            if (!EMRArchiveAdaperUse.storageEmrFile(strArgs))
            {
                MessageBox.Show("将考勤摘要保存回服务器出错!");
                return;
            }
            
            // 保存文本格式
            if (!emrpadMrBrief.PadFileSaveAs(EmrSysPubVar.getTempFileFullName(), 5, 0))
            {
                MessageBox.Show("保存文件出错。");
                return;
            }
            
            // 传送文本文件
            object[] strArgstxt = new object[3];
            strArgstxt[0] = 0;
            strArgstxt[1] = "blzy.txt";
            strArgstxt[2] = EmrSysPubVar.getCurPatientID();
            
            if (!EMRArchiveAdaperUse.storageEmrFile(strArgstxt))
            {
                MessageBox.Show("将考勤摘要txt文件保存回服务器出错!");
                return;
            }
            else
            {
                MessageBox.Show("考勤摘要保存成功!");
            }
        }

        private void chkExamAll_CheckedChanged(object sender, EventArgs e)
        {
            DataSet objDataSetExamMaster = new DataSet();
            objDataSetExamMaster = EmrSysWebservicesUse.myEmrGetExamMaster(EmrSysPubVar.getCurPatientID(), 
                chkExamAll.Checked ? 0 : EmrSysPubVar.getCurPatientVisitID());
                
            if (objDataSetExamMaster.Tables.Count > 0)
            {
                DataTable objTable = objDataSetExamMaster.Tables[0];
                dgvExam.DataSource = objTable.DefaultView;
            }
        }

        private void chkLabAll_CheckedChanged(object sender, EventArgs e)
        {
            DataSet objDataSetLabTestMaster = new DataSet();
            objDataSetLabTestMaster = EmrSysWebservicesUse.myEmrGetLabDataSetByPatientID(EmrSysPubVar.getCurPatientID(), 
                chkLabAll.Checked ? 0 : EmrSysPubVar.getCurPatientVisitID());
                
            if (objDataSetLabTestMaster.Tables.Count > 0)
            {
                DataTable objTable = objDataSetLabTestMaster.Tables[0];
                dgvLabTestMaster.DataSource = objTable.DefaultView;
            }
        }

        private DataSet getVitalSignsRec()
        {
            // 获取生命体征记录的实现
            return new DataSet();
        }

        private object m_objPad; // 假设的Pad对象
    }
}